package com.gmm.springboot.admin.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@Scope("singleton")
@RequestMapping("/")
public class IndexController {

	@RequestMapping(value = "/")
	public String index() {
		return "flower";
	}


	@RequestMapping(value = "/getAtomResult")
	@ResponseBody
	public String getAtomResult(@RequestParam(value = "atom", required = false) String atom, @RequestParam(value = "molecule", required = false) String molecule) {

		if (StringUtils.isBlank(atom)){
			return "左边的组合不能为空哦";
		}

		if (StringUtils.isBlank(molecule)){
			return "值不能为空哦";
		}

		int count = 0;

		String numString = atom.trim().replaceAll("\n", ",").replaceAll("，", ",").replaceAll(" ", ",").replaceAll(",,", ",");
		String total = molecule.trim();
		float totalFloat = Float.valueOf(total);
		String[] numArr = numString.split(",");
		int length = numArr.length;
		float[] floatArr = new float[length];
		for (int i = 0; i< numArr.length; i++){
			floatArr[i] = Float.valueOf(numArr[i]);
		}
		String[] param = new String[length];
		for (int i = 1; i<=length;i++){
			param[i-1] = String.valueOf(i);
		}

		String resultString = "可能的结果有：";

		for (int num = 1; num<= length; num++){
			List<String> result  = combine(param, num);

			for (int i =0; i< result.size(); i++){
				String temp = result.get(i);
				StringBuilder sb = new StringBuilder();
				String[] numTemp = temp.split(",");
				float tempTotal = 0.0f;
				int index = 0;
				for (int j = 0; j < numTemp.length; j++){
					index = Integer.valueOf(numTemp[j]) -1;
					tempTotal += Float.valueOf(numArr[index]);
					sb.append(numArr[index]+",");
				}
				if (tempTotal == totalFloat){
					resultString += sb.toString() + " | ";
					count++;
				}
			}

		}
		if (count == 0){
			return "哦，no，没有数字可以组成哦";
		}
		return resultString;
	}


	private static List<String> combine(String[] a, int num) {
		List<String> list = new ArrayList<>();
		StringBuffer sb = new StringBuffer();
		String[] b = new String[a.length];
		for (int i = 0; i < b.length; i++) {
			if (i < num) {
				b[i] = "1";
			} else{
				b[i] = "0";
			}

		}

		int point = 0;
		int nextPoint = 0;
		int count = 0;
		int sum = 0;
		String temp = "1";
		while (true) {
			// 判断是否全部移位完毕
			for (int i = b.length - 1; i >= b.length - num; i--) {
				if (b[i].equals("1")){
					sum += 1;
				}

			}
			// 根据移位生成数据
			for (int i = 0; i < b.length; i++) {
				if (b[i].equals("1")) {
					point = i;
					sb.append(a[point]);
					sb.append(",");
					count++;
					if (count == num){
						break;
					}

				}
			}
			// 往返回值列表添加数据
			list.add(sb.toString());

			// 当数组的最后num位全部为1 退出
			if (sum == num) {
				break;
			}
			sum = 0;

			// 修改从左往右第一个10变成01
			for (int i = 0; i < b.length - 1; i++) {
				if (b[i].equals("1") && b[i + 1].equals("0")) {
					point = i;
					nextPoint = i + 1;
					b[point] = "0";
					b[nextPoint] = "1";
					break;
				}
			}
			// 将 i-point个元素的1往前移动 0往后移动
			for (int i = 0; i < point - 1; i++){
				for (int j = i; j < point - 1; j++) {
					if (b[i].equals("0")) {
						temp = b[i];
						b[i] = b[j + 1];
						b[j + 1] = temp;
					}
				}
			}

			sb.setLength(0);
			count = 0;
		}
		return list;

	}
}